using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration;

namespace OpenScrum.db
{
    public class BacklogItem
    {
        #region Properties

        public int BacklogItemId { get; set; }  
        [Required]
        [MaxLength(1000)]
        public string Name { get; set; }
        public string Description { get; set; } 
        public DateTime CreatedDate { get; set; }
        public int CreatedById { get; set; }
        public int? OwnedById { get; set; }
        public int ProjectId { get; set; }
        public int? SwimlaneId { get; set; }

        #endregion

        #region Navigation Properties
        
        public virtual Project Project { get; set; }        
        public virtual Swimlane CurrentSwimlane { get; set; }  
        public virtual TeamMember CreatedBy { get; set; }   
        public virtual TeamMember OwnedBy { get; set; }        
        public virtual ICollection<BacklogItemTest> Tests { get; set; }

        #endregion

        #region Constructor

        public BacklogItem()
        {
            CreatedDate = DateTime.Now;
        }

        #endregion
    }

    class BacklogItemConfiguration : EntityTypeConfiguration<BacklogItem>
    {
        internal BacklogItemConfiguration()
        {
            // required one-to-many between team member and backlog item
            this
                .HasRequired(m => m.CreatedBy)
                .WithMany(b => b.BacklogItemsCreated)
                .HasForeignKey(bli => bli.CreatedById);

            // optional one-to-many between team member and backlog item
            this
                .HasOptional(m => m.OwnedBy)
                .WithMany(b => b.BacklogItemsOwned)
                .HasForeignKey(bli => bli.OwnedById);
            //.WillCascadeOnDelete(false); // this is needed for two required columns referencing one table

            this.HasOptional(m => m.CurrentSwimlane)
                .WithMany()
                .HasForeignKey(k => k.SwimlaneId);
        }
    }
}